package com.quickask.operate.data;

/**
 * Created by Administrator on 2015/9/28.
 */
public class Constants {

    //shareSDK短信验证APPKEY
    public static final String SHARE_SDK_SMS_APPKEY = "a22acc60ca00";

    //七牛云存储KEY
    public static final String QINIU_ACCESS_KEY = "6HTJHKkOMci8pDx2DyXTGuSiMgyVhlR7Pht1FDxj";
    public static final String QINIU_SECRET_KEY = "pue5ma-8yAaUh2sBed6J1RBxikU6K3M3X7er_03G";
    public static final String QINIU_BUCKET_NAME = "quickask";

    //token
    public static final int REFRESH_TOKEN_EXPIRE_TIME = 3600;


    //地球半径
    public static final double EARTH_RADIUS = 6378137.0D;

    //经纬度误差范围
    public static final double LOCATE_DEVIATION = 0.3D;
    //距离误差
    public static final double DISTANCE_DEVIATION = 5000;

    //geo字符串截取长度
    public static final int GEO_STR_LENGTH = 5;


    /**
     * 计算俩经纬度间的距离(误差较小)
     *
     * @param lat1 纬度
     * @param lon1 经度
     * @param lat2 纬度
     * @param lon2 经度
     * @return
     */
    public static double computeDistance(double lat1, double lon1, double lat2, double lon2) {
        int MAXITERS = 20;

        lat1 *= 0.0174532925199433D;
        lat2 *= 0.0174532925199433D;
        lon1 *= 0.0174532925199433D;
        lon2 *= 0.0174532925199433D;
        double a = 6378137.0D;
        double b = 6356752.3141999999D;
        double f = (a - b) / a;
        double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);

        double L = lon2 - lon1;
        double A = 0.0D;
        double U1 = Math.atan((1.0D - f) * Math.tan(lat1));
        double U2 = Math.atan((1.0D - f) * Math.tan(lat2));

        double cosU1 = Math.cos(U1);
        double cosU2 = Math.cos(U2);
        double sinU1 = Math.sin(U1);
        double sinU2 = Math.sin(U2);
        double cosU1cosU2 = cosU1 * cosU2;
        double sinU1sinU2 = sinU1 * sinU2;

        double sigma = 0.0D;
        double deltaSigma = 0.0D;
        double cosSqAlpha = 0.0D;
        double cos2SM = 0.0D;
        double cosSigma = 0.0D;
        double sinSigma = 0.0D;
        double cosLambda = 0.0D;
        double sinLambda = 0.0D;

        double lambda = L;
        for (int iter = 0; iter < MAXITERS; iter++) {
            double lambdaOrig = lambda;
            cosLambda = Math.cos(lambda);
            sinLambda = Math.sin(lambda);
            double t1 = cosU2 * sinLambda;
            double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
            double sinSqSigma = t1 * t1 + t2 * t2;
            sinSigma = Math.sqrt(sinSqSigma);
            cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda;
            sigma = Math.atan2(sinSigma, cosSigma);
            double sinAlpha = sinSigma == 0.0D ? 0.0D : cosU1cosU2 * sinLambda / sinSigma;

            cosSqAlpha = 1.0D - sinAlpha * sinAlpha;
            cos2SM = cosSqAlpha == 0.0D ? 0.0D : cosSigma - 2.0D * sinU1sinU2 / cosSqAlpha;

            double uSquared = cosSqAlpha * aSqMinusBSqOverBSq;
            A = 1.0D + uSquared / 16384.0D * (4096.0D + uSquared * (-768.0D + uSquared * (320.0D - 175.0D * uSquared)));

            double B = uSquared / 1024.0D * (256.0D + uSquared * (-128.0D + uSquared * (74.0D - 47.0D * uSquared)));

            double C = f / 16.0D * cosSqAlpha * (4.0D + f * (4.0D - 3.0D * cosSqAlpha));

            double cos2SMSq = cos2SM * cos2SM;
            deltaSigma = B * sinSigma * (cos2SM + B / 4.0D * (cosSigma * (-1.0D + 2.0D * cos2SMSq) - B / 6.0D * cos2SM * (-3.0D + 4.0D * sinSigma * sinSigma) * (-3.0D + 4.0D * cos2SMSq)));

            lambda = L + (1.0D - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SM + C * cosSigma * (-1.0D + 2.0D * cos2SM * cos2SM)));

            double delta = (lambda - lambdaOrig) / lambda;
            if (Math.abs(delta) < 1.0E-012D) {
                break;
            }
        }
        return b * A * (sigma - deltaSigma);
    }

    private static double rad(double d) {
        return d * 3.141592653589793D / 180.0D;
    }


    /**
     * 计算俩经纬度间的距离(误差一般)
     *
     * @param lat1 纬度
     * @param lon1 经度
     * @param lat2 纬度
     * @param lon2 经度
     * @return
     */
    public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2.0D * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2.0D), 2.0D) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2.0D), 2.0D)));
        s *= EARTH_RADIUS;

        return s;
    }

}
